package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.ProjectInfo;
import com.jr.service.IProjectInfoService;
import com.jr.service.impl.ProjectInfoServiceImpl;
import com.jr.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/projectInfo/*")
public class ProjectInfoController extends HttpServlet {

    private IProjectInfoService ips=new ProjectInfoServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        int index=requestURI.lastIndexOf("/")+1;
        String mName = requestURI.substring(index);
        if(mName.length()==0){
            mName="showdt";
        }
        Class<ProjectInfoController> cla = ProjectInfoController.class;
        Method method = null;
        try {
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);

          /*  response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");*/

            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void showdt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        response.setContentType("application/json;charset=utf-8");
                String status = request.getParameter("status");
                int proStatus=0;
                if(status==null){
                    proStatus=-1;
                }else{
                    proStatus=Integer.parseInt(status);
                }
                PageHelper ph=new PageHelper();
        ph.setDateCount(ips.queryDataCount(proStatus)); //获得总条数
            String indexpage = request.getParameter("indexpage");
            if(indexpage==null){
                ph.setPageIndex(1);//当前是第几页
            }else{
                ph.setPageIndex(Integer.parseInt(indexpage));//当前是第几页
            }
            ArrayList<ProjectInfo> projectInfos = ips.queryByStatus(proStatus,ph);
        ph.setListIndex(projectInfos);
         response.getWriter().println(new Gson().toJson(ph));

    }

    public void queryByPid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        response.setContentType("application/json;charset=utf-8");
        int pid=Integer.parseInt(request.getParameter("pid"));
        ProjectInfo projectInfo = ips.queryByPid(pid);
        response.getWriter().println(new Gson().toJson(projectInfo));

    }

    public void updatePro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

       int status=Integer.parseInt(request.getParameter("status"));
       int pid=Integer.parseInt(request.getParameter("pid"));
           ProjectInfo pro=new ProjectInfo();
           pro.setPid(pid);
           pro.setStatus(status);
       boolean b = ips.updateProject(pro);
       response.getWriter().println(new Gson().toJson(b));

    }
}